//
//  InternalVehicleTrajectoryListViewController.m
//  Runone
//
//  Created by ekkaLyn on 2017/3/17.
//  Copyright © 2017年 runone. All rights reserved.
//

#import "InternalVehicleTrajectoryListViewController.h"
#import "RequestHelper.h"
#import "MJExtension.h"
#import "CarListModel.h"
#import "AllLastestVehicleDynamic.h"
#import "HistoryViewController.h"
#import "CarCell.h"
#import "UIView+Message.h"
#import "PromptUtil.h"
#import "SVProgressHUD.h"
#import "GetBusDangeroursHistoryCountModel.h"
#import "BDVehicleCarListModel.h"
#import "AllLastestVehicleDynamic.h"

@interface InternalVehicleTrajectoryListViewController ()

//@property(nonatomic,strong)NSTimer *timer;
@property (nonatomic, strong) NSArray *trajectoryArray;
@property (nonatomic, strong) NSMutableArray *forArray;
@property (nonatomic, assign) NSInteger currentPageIndex;
@property (nonatomic, strong) BDVehicleCarListModel *listModel;
@property (nonatomic, strong) GetBusDangeroursHistoryCountModel *countModel;
@end

@implementation InternalVehicleTrajectoryListViewController

#pragma       -----------内部车辆查询结果

- (void)viewDidLoad {
    [super viewDidLoad];
    //    self.title=@"查询结果";
    _listModel   = [[BDVehicleCarListModel alloc] init];
    _countModel  = [[GetBusDangeroursHistoryCountModel alloc] init];
    
    _forArray = [[NSMutableArray alloc] init];
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    label.text = @"内部车辆查询结果";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=label;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CarCell" bundle:nil] forCellReuseIdentifier:@"CarCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self requestCarList];
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}

-(NSString *)jsonstr {
    NSArray *arr=@[@{@"key":@"VehicleNo",@"value":_carNum},@{@"key":@"StartDate",@"value":_beginTime},@{@"key":@"EndDate",@"value":_endTime}];
    NSString *str=[arr mj_JSONString];
    return str;
}

//加载数据
-(void)requestCarList {
    //    __weak typeof(self) weakSelf = self;
    [SVProgressHUD showWithStatus:@"正在加载中..."];
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetCarHistoryURL;
    getCurrentEventModel.DataValue=[self jsonstr];
    getCurrentEventModel.UpdatedTime=@"";
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"GetIntVclDynamicHistoryTrajectory";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        [SVProgressHUD dismiss];
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
            _trajectoryArray = [BDVehicleCarListModel mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];
            [self.tableView reloadData];
        }
    };
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.trajectoryArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CarCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CarCell" forIndexPath:indexPath];
    BDVehicleCarListModel *model = self.trajectoryArray[indexPath.row];
    cell.CarNumber.text  = _carNum;
    cell.BenginTime.text = model.BeginTime;
    cell.EndTime.text    = model.EndTime;
    cell.carTypeL.hidden = YES;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 116;
}


- (NSString *)forOneTrace:(NSString *)vehicleNO beginTime:(NSString *)bTimeStr endTime:(NSString *)eTimeStr {
    NSArray *arr=@[@{@"key":@"VehicleNo",@"value":vehicleNO},@{@"key":@"StartDate",@"value":bTimeStr},@{@"key":@"EndDate",@"value":eTimeStr}];
    NSString *str=[arr mj_JSONString];
    return str;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _currentPageIndex = 0;
    [self.forArray removeAllObjects];
    [SVProgressHUD showWithStatus:@"正在加载中..."];
    _listModel = self.trajectoryArray[indexPath.row];
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetCarHistoryURL;
    getCurrentEventModel.DataValue   = [self forOneTrace:_carNum beginTime:_listModel.BeginTime endTime:_listModel.EndTime];
    getCurrentEventModel.UpdatedTime = [requestHelper getCurrentTime];
    getCurrentEventModel.SystemCode  = @"GuangleHighway";
    getCurrentEventModel.MethodName  = @"GetIntVclDynamicHistoryCount";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        if ([requestOutginseng.Code isEqualToString:@"001"] ) {
            _countModel = [GetBusDangeroursHistoryCountModel mj_objectWithKeyValues:requestOutginseng.DataValue];
            if (_countModel.AllCount > 0) {
                //for (int i = 0; i < _countModel.PageSize; i ++) {
                    
                    [self requestForTrac:_countModel andBT:_listModel.BeginTime ET:_listModel.EndTime];
                //}
            }else {
                [SVProgressHUD showErrorWithStatus:@"没有相关的轨迹信息"];
            }
        }else {
            [SVProgressHUD showSuccessWithStatus:@"请重新尝试"];
        }
    };
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}

//跳转下一个控制器
- (void) pushToNextViewController {
    
    if (self.forArray.count > 0 ) {
        HistoryViewController *vc = [[HistoryViewController alloc] init];
        vc.arr = self.forArray;
        //                vc.historyModel = self.CarDetailArray[index];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(NSString *)everyCountTrac:(NSString *)vehicleNO BegineTime:(NSString *)bTime EndTime:(NSString *)eTime PageIndex:(NSString *)pSize PageCount:(NSString *)pCount  {
    NSArray *arr=@[@{@"key":@"VehicleNo",@"value":vehicleNO},@{@"key":@"StartDate",@"value":bTime},@{@"key":@"EndDate",@"value":eTime}, @{@"key":@"PageIndex",@"value":pSize},@{@"key":@"PageCount",@"value":pCount}];
    NSString *str=[arr mj_JSONString];
    return str;
}
//AllLastestVehicleDynamic
//加载数据
- (void) requestForTrac:(GetBusDangeroursHistoryCountModel *)countModel andBT:(NSString *)bTime ET:(NSString *)eTime {
    _currentPageIndex += 1;
    RequestHelper *requestHelper = [[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetCarHistoryURL;
    getCurrentEventModel.DataValue   = [self everyCountTrac:_carNum BegineTime:_listModel.BeginTime EndTime:_listModel.EndTime PageIndex:[NSString stringWithFormat:@"%ld",(long)_currentPageIndex] PageCount:[NSString stringWithFormat:@"%ld",(long)_countModel.PageCount]];
    getCurrentEventModel.UpdatedTime = [requestHelper getCurrentTime];
    getCurrentEventModel.SystemCode  = @"GuangleHighway";
    getCurrentEventModel.MethodName  = @"GetIntVclDynamicHistoryList";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
            NSArray *array = [AllLastestVehicleDynamic mj_objectArrayWithKeyValuesArray:requestOutginseng.DataValue];

            [self.forArray addObjectsFromArray:array];
            if (_currentPageIndex == _countModel.PageSize) {
                [SVProgressHUD dismiss];
                [self pushToNextViewController];
            }else if (_currentPageIndex < _countModel.PageSize) {
                [self requestForTrac:_countModel andBT:bTime ET:eTime];
            }
        }else {
            [SVProgressHUD showErrorWithStatus:@"网络问题，请重新尝试"];
        }
    };
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}


@end
